/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import bo.Product_BUS;
import com.opensymphony.xwork2.ActionSupport;
import dto.ProductDTO;
import java.util.ArrayList;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

/**
 *
 * @author Tam Nguyen
 */
public class CartAction extends ActionSupport implements SessionAware, ServletRequestAware{
    private Map session;
    private HttpServletRequest request;
    private ArrayList<dto.ProductDTO> CartList ;
    private String product_id;
    private int quantity;
    private boolean Update = false;
     public void setUpdate(boolean Update) {
        this.Update = true;
    }

    public String getProduct_id() {
        return product_id;
    }

    public void setProduct_id(String product_id) {
        this.product_id = product_id;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public ArrayList<ProductDTO> getCartList() {
        return CartList;
    }

    public void setCartList(ArrayList<ProductDTO> CartList) {
        this.CartList = CartList;
    }
    
    public CartAction() {
    }
    
    public String execute() throws Exception {
        if(request.getParameter("action") != null)
        {
            if(request.getParameter("action").toString().equals("Add"))
            {
                if(request.getParameter("ID") !=null)
                {
                    String id = request.getParameter("ID").toString();
                    if(session.get("Cart") == null)
                    {
                        ArrayList<ProductDTO> cart = new ArrayList();
                        Product_BUS bus = new Product_BUS();
                        ProductDTO pr = new ProductDTO();
                        pr = bus.GetOneProduct(id);
                        cart.add(pr);
                        session.put("Cart",cart);

                    }
                    if(session.get("Cart")!= null)
                    {
                        ArrayList<ProductDTO> cart = new ArrayList<ProductDTO>();
                        cart = (ArrayList<ProductDTO>) session.get("Cart");
                        boolean flag = true;
                        for (int i =0;i<cart.size();i++)
                        {
                            ProductDTO pr = new ProductDTO();
                            pr = cart.get(i);
                            if(pr.getProduct_id().equals(id))
                            {
                                pr.setQuantity_cart(pr.getQuantity_cart()+1);
                                flag = false;
                                break;
                            }
                                
                        }
                        if(flag == true)
                        {
                                Product_BUS bus = new Product_BUS();
                                ProductDTO pr = new ProductDTO();
                                pr = bus.GetOneProduct(id);
                                cart.add(pr);
                        }
                        
                    }
                    
                }
            }
            if(request.getParameter("action").toString().equals("Remove"))
                    {
                        if(request.getParameter("ID") != null)
                        {
                            String id = request.getParameter("ID").toString();
                            ArrayList<ProductDTO> cart = new ArrayList<ProductDTO>();
                            cart = (ArrayList<ProductDTO>)session.get("Cart");
                            for(int i=0;i<cart.size();i++)
                            {
                                ProductDTO pr =new ProductDTO();
                                pr = cart.get(i);
                                if(id.equals(pr.getProduct_id()))
                                {
                                    cart.remove(pr);
                                    
                                }
                            }
                            session.put("Cart",cart);
                        }
                        
                    }
            
            CartList = (ArrayList<ProductDTO>)session.get("Cart");
            
        }
        if (Update == true) 
        {
                Update();
                return ("success");
            }
        return "success";
    }
    public void Update()
    {
        ArrayList<ProductDTO> cart = new ArrayList<ProductDTO>();
        cart = (ArrayList<ProductDTO>)session.get("Cart");
        for(int i =0;i<cart.size();i++)
        {
            ProductDTO pr = new ProductDTO();
            pr = cart.get(i);
            if(pr.getProduct_id().equals(product_id))
            {
                pr.setQuantity_cart(quantity);
            }
        }
        session.put("Cart",cart);
    }
    @Override
    public void setSession(Map<String, Object> map) {
        session = map;
    }

    @Override
    public void setServletRequest(HttpServletRequest hsr) {
        request = hsr;
    }
}
